home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / dbu / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  10.4 KB  |  576 lines

  1. # include    <pv.h>
  2. # include    <ingres.h>
  3. # include    <aux.h>
  4. # include    <catalog.h>
  5. # include    <access.h>
  6. # include    <func.h>
  7. # include    <signal.h>
  8. # include    <errors.h>
  9.  
  10.  
  11. extern short     tTdbu[100];
  12. extern int    help();
  13. extern int    null_fn();
  14.  
  15. struct fn_def HelpFn =
  16. {
  17.     "HELP",
  18.     help,
  19.     null_fn,        /* initialization function */
  20.     null_fn,
  21.     NULL,
  22.     0,
  23.     tTdbu,
  24.     100,
  25.     'Z',
  26.     0
  27. };
  28.  
  29.  
  30. /*
  31. **  HELP - Provide Information to User
  32. **
  33. **    Arguments:
  34. **        pv[i] - code
  35. **            RELINFO - print relation information
  36. **            MANSEC - print manual section
  37. **            DELIMLIST - print delim information
  38. **            RELIST - print relation list
  39. **            ALLRELINFO - print relation info for all accessible 
  40. **                        relations
  41. **
  42. **        pv[i+1] - name of entity for modes 0 or 1
  43. **
  44. **    Trace Flags:
  45. **        44
  46. */
  47.  
  48. help(parmc, parmv)
  49. int    parmc;
  50. PARM    parmv[];
  51. {
  52.     DESC        des;
  53.     int        mode;
  54.     register PARM    *pv;
  55.     register int    ret;
  56.     int        i;
  57.  
  58. # ifdef    xZTR1
  59.     if (tTf(44, -1))
  60.     {
  61.         printf(">>help\n");
  62.         if (tTf(44, 0))
  63.             prvect(parmc, parmv);
  64.     }
  65. # endif
  66.  
  67.     ret = 0;
  68.     pv = parmv;
  69.     getuser(-1);    /* init getuser for modes RELINFO & MANSEC */
  70.     while (pv->pv_type != PV_EOF)
  71.     {
  72.         mode = (pv++)->pv_val.pv_int;
  73.         if (mode < RELIST && pv->pv_type == PV_EOF)
  74.             syserr("help: mode %d no val", mode);
  75.  
  76. #        ifdef xZTR1
  77.         if (tTf(44, -1))
  78.         {
  79.             printf("help %d", mode);
  80.             if (mode != RELIST)
  81.                 printf(" %s", pv->pv_val.pv_str);
  82.             putchar('\n');
  83.         }
  84. #        endif
  85.         switch (mode)
  86.         {
  87.  
  88.           case RELINFO:    /* help relation */
  89.             if (!openr(&des, OR_RELTID, pv->pv_val.pv_str))
  90.             {
  91.                 rel_fmt(&des);
  92.                 pv->pv_val.pv_str = NULL;
  93.             }
  94.             pv++;
  95.             break;
  96.  
  97.           case MANSEC:    /* help manual section */
  98.             if (man(pv->pv_val.pv_str))
  99.                 pv->pv_val.pv_str = NULL;
  100.             pv++;
  101.             break;
  102.  
  103.           case DELLIST:    /* help delim */
  104.             if (!openr(&des, OR_READ, "rdelim"))
  105.             {
  106.                 if ((i = helpdelim(pv->pv_val.pv_str,&des)) == -1)
  107.                     return(error(RDELIMERR, pv->pv_val.pv_str, 0));
  108.                 closer(&des);
  109.                 pv->pv_val.pv_str = NULL;
  110.             }
  111.             pv++;
  112.             break;
  113.  
  114.           case RELIST:
  115.           case ALLRELINFO:
  116.             relpr(mode);
  117.             break;
  118.  
  119.           case ALLDELLIST:
  120.             if (!openr(&des, OR_READ, "rdelim"))
  121.             {
  122.                 if ((i = alldelims(&des)) == -1)
  123.                     return(error(RDELIMERR, pv->pv_val.pv_str, 0));
  124.                 closer(&des);
  125.                 pv->pv_val.pv_str = NULL;
  126.             }
  127.             pv++;
  128.             break;
  129.  
  130.           default:
  131.             syserr("HELP: mode %d", mode);
  132.         }
  133.     }
  134.     getuser(0);    /* close getuser in case mode RELINFO or MANSEC */
  135.  
  136.     /* now rescan for error messages */
  137.     pv = parmv;
  138.     while (pv->pv_type != PV_EOF)
  139.     {
  140.         mode = (pv++)->pv_val.pv_int;
  141.  
  142.         if (mode < RELIST)
  143.         {
  144.             if (pv->pv_val.pv_str != NULL)
  145.                 ret = nferror(NORELEXIST + mode, pv->pv_val.pv_str, 0);
  146.             pv++;
  147.         }
  148.     }
  149.     return (ret);
  150. }
  151.  
  152.  
  153. /*
  154. **  Nroff Manual Section
  155. **
  156. **    The manual section given by 'name' is nroff'ed.  Returns one
  157. **    on success, zero if the manual section is not found.
  158. **
  159. **    Uses trace flag 11
  160. */
  161.  
  162. int
  163. man(name)
  164. char    *name;
  165. {
  166.     char        manual[100];
  167.     register int    i;
  168.     int        stat;
  169.     char        name_nr[18];
  170.     register char    *naa;
  171.     extern char    *ztack();
  172.  
  173.     if (length(name) > 14)
  174.         return (0);
  175.  
  176.     /* a null manual name gives table of contents */
  177.     if (name[0] == 0)
  178.         smove("../toc.nr", name_nr);
  179.     else
  180.         concat(name, ".nr", name_nr);
  181.  
  182.     concat(ztack(Pathname, "/doc/quel/"), name_nr, manual);
  183.     if ((i = open(manual, O_RDONLY)) < 0)
  184.     {
  185.         /* try a unix command instead */
  186.         concat(ztack(Pathname, "/doc/unix/"), name_nr, manual);
  187.         if ((i = open(manual, O_RDONLY)) < 0)
  188.             return (0);
  189.     }
  190.     if (close(i))
  191.         syserr("cannot close %s", manual);
  192.     ruboff(0);    /* wait for child's death if rubout occures */
  193.     i = fork();
  194.     if (i == 0)
  195.     {
  196.         signal(SIGINT, SIG_DFL);    /* die on rubout */
  197.         setuid(getuid());
  198.         setgid(getgid());
  199.         naa = ztack(Pathname, "/doc/iaa");
  200.         execl("/bin/nroff", "nroff", naa, manual, 0);
  201.         execl("/usr/bin/nroff", "nroff", naa, manual, 0);
  202.         syserr("help: exec: nroff");
  203.     }
  204.     /* wait for nroff if fork succeeded */
  205.     if (i > 0)
  206.         fullwait(i, "help: nroff");
  207.     rubon();
  208.     return (1);
  209. }
  210.  
  211.  
  212. /*
  213. **  PRINT DATABASE INFORMATION
  214. **
  215. **    Prints a list of all the relations in the database, together
  216. **    with their owner.
  217. **
  218. **    Uses trace flag 12
  219. */
  220.  
  221. relpr(mode)
  222. int    mode;
  223. {
  224.     extern DESC    Reldes;
  225.     register DESC    *d;
  226.     register int            i;
  227.     register char            *cp;
  228.     struct tup_id            limtid, tid;
  229.     char                buf[MAXLINE + 1];
  230.     char                lastuser[2];
  231.     struct relation            rel;
  232.  
  233.     opencatalog("relation", OR_READ);
  234.     d = &Reldes;
  235.     if (i = find(d, NOKEY, &tid, &limtid))
  236.         syserr("help: relpr: find %d", i);
  237.  
  238.     lastuser[0] = '\0';
  239.  
  240.     if (mode == RELIST)
  241.         printf("\n relation name     relation owner\n\n");
  242.  
  243.     while ((i = get(d, &tid, &limtid, &rel, 1)) == 0)
  244.     {
  245.         if (mode == RELIST)
  246.         {
  247.             if (!bequal(lastuser, rel.relowner, 2))
  248.             {
  249.                 if (getuser(rel.relowner, buf))
  250.                 {
  251.                     /* cant find user code */
  252.                     bmove("  ", buf, 2);
  253.                     cp = &buf[2];
  254.                     bmove(rel.relowner, cp, 2);
  255.                     cp = &cp[2];
  256.                     *cp = '\0';
  257.                 }
  258.                 else
  259.                 {
  260.                     for (cp = buf; *cp != ':'; cp++)
  261.                         ;
  262.                     *cp = '\0';
  263.                 }
  264.                 bmove(rel.relowner, lastuser, 2);
  265.             }
  266.             printf(" %.12s      %s\n", rel.relid, buf);
  267.         }
  268.         else
  269.         {
  270.             if ((rel.relstat & S_CATALOG) || bequal("_SYS", rel.relid, 4))
  271.                 continue;
  272.             if (bequal(Usercode, rel.relowner, UCODE_SZ) || bequal(Admin.adhdr.adowner, rel.relowner, 2))
  273.                 rel_fmt(&rel);
  274.         }
  275.     }
  276.  
  277.     if (i < 0)
  278.         syserr("help: relpr: get %d", i);
  279.     if (mode == RELIST)
  280.         printf("\n");
  281.     return (0);
  282. }
  283.  
  284.  
  285.  
  286. /*
  287. **  Print Relation Information
  288. **
  289. **    Prints detailed information regarding the relation.
  290. **
  291. **    Uses trace flag 13
  292. */
  293.  
  294. rel_fmt(r)
  295. register struct relation    *r;
  296. {
  297.     struct tup_id        limtid, tid;
  298.     char            buf[MAXLINE + 1];
  299.     struct attribute    att;
  300.     struct index        indkey, ind;
  301.     register int        i;
  302.     int            j;
  303.     extern DESC        Attdes, Inddes;
  304.     char            *trim_relname();
  305.  
  306.     printf("\nRelation:\t\t%s\n", trim_relname(r->relid));
  307.     i = getuser(r->relowner, buf);
  308.     if (i)
  309.     {
  310.         smove("(xx)", buf);
  311.         bmove(r->relowner, &buf[1], 2);
  312.     }
  313.     else
  314.     {
  315.         for (i = 0; buf[i] != ':'; i++)
  316.             continue;
  317.         buf[i] = 0;
  318.     }
  319.     printf("Owner:\t\t\t%s\n", buf);
  320.     printf("Tuple width:\t\t%d\n", r->relwid);
  321.     if (r->relsave != 0)
  322.     {
  323.         printf("Saved until:\t\t%s", ctime(&r->relsave));
  324.     }
  325.     if ((r->relstat & S_VIEW) == 0)
  326.     {
  327.         printf("Number of tuples:\t%ld\n", r->reltups);
  328.         printf("Storage structure:\t");
  329.         i = r->relspec;
  330.         if (i < 0)
  331.         {
  332.             printf("compressed ");
  333.             i = -i;
  334.         }
  335.         switch (i)
  336.         {
  337.     
  338.           case M_HEAP:
  339.             printf("paged heap\n");
  340.             break;
  341.     
  342.           case M_ISAM:
  343.             printf("ISAM file\n");
  344.             break;
  345.     
  346.           case M_HASH:
  347.             printf("random hash\n");
  348.             break;
  349.     
  350.           default:
  351.             printf("unknown structure %d\n", i);
  352.             break;
  353.     
  354.         }
  355.     }
  356.  
  357.     printf("Relation type:\t\t");
  358.     if (r->relstat & S_CATALOG)
  359.         printf("system catalog\n");
  360.     else if (r->relstat & S_VIEW)
  361.         printf("view\n");
  362.     else
  363.         if (r->reldim > 0)
  364.             printf("ordered relation\n");
  365.         else if (r->relindxd < 0)
  366.         {
  367.             printf("secondary index on ");
  368.             opencatalog("indexes", OR_READ);
  369.             setkey(&Inddes, &indkey, r->relowner, IOWNERP);
  370.             setkey(&Inddes, &indkey, r->relid, IRELIDI);
  371.             if (!getequal(&Inddes, &indkey, &ind, &tid))
  372.                 printf("%s\n", trim_relname(ind.irelidp));
  373.             else
  374.                 printf("unknown relation\n");
  375.         }
  376.         else
  377.         {
  378.             if (r->relstat & S_DISTRIBUTED)
  379.                 printf("distributed ");
  380.             printf("user relation\n");
  381.         }
  382.     if (r->relindxd > 0)
  383.     {
  384.         printf("Secondary Indices:\t");
  385.         opencatalog("indexes", OR_READ);
  386.         setkey(&Inddes, &indkey, r->relid, IRELIDP);
  387.         setkey(&Inddes, &indkey, r->relowner, IOWNERP);
  388.         if (i = find(&Inddes, EXACTKEY, &tid, &limtid, &indkey))
  389.             syserr("help: find %d indexes", i);
  390.         j = FALSE;
  391.         while ((i = get(&Inddes, &tid, &limtid, &ind, 1)) == 0)
  392.         {
  393.             if (!bequal(&indkey, &ind, MAXNAME + 2))
  394.                 continue;
  395.             if (j)
  396.                 printf(", ");
  397.             j =TRUE;
  398.             printf("%s", trim_relname(ind.irelidi));
  399.         }
  400.         if (i < 0)
  401.             syserr("help:get indexes %d", i);
  402.         if (!j)
  403.             printf("unknown");
  404.     }
  405.     printf("\n");
  406.  
  407.     opencatalog("attribute", OR_READ);
  408.     printf("\n attribute name    type  length  keyno.\n\n");
  409.     seq_init(&Attdes, r);
  410.     while (seq_attributes(&Attdes, r, &att))
  411.     {
  412.         printf(" %.12s        %c%8d",
  413.             att.attname, att.attfrmt, att.attfrml & I1MASK);
  414.         if (att.attxtra)
  415.             printf("%7d", att.attxtra);
  416.         printf("\n");
  417.     }
  418.  
  419.     printf("\n");
  420.     return (0);
  421. }
  422.  
  423. /*
  424. **    HELPDELIM - print all delims presently defined.
  425. **
  426. **        Parameters:
  427. **            group - the group of delims to print
  428. **
  429. **        Returns:
  430. **            0 - if successful
  431. **            -1 - if relation not found
  432. */
  433. helpdelim(group,des)
  434. char    *group;
  435. DESC    *des;
  436. {
  437.     DELIM_TUP    tuple;
  438.     TID        lotid,hitid;
  439.     int        notfound = 1;
  440.     char        delim[12];
  441.     int        start = 0;
  442.     int        begin = 1;
  443.     char        dstring[1024];
  444.     char        *dptr;
  445.     int        found=0;
  446.  
  447.     if (find(des,LRANGEKEY, &lotid, &hitid, group) < 0)
  448.         return(-1);
  449.     find(des,HRANGEKEY, &lotid, &hitid, group);
  450.     printf("\n \t>>>>  %s  <<<<\n", group);
  451.  
  452.     while (!get(des, &lotid, &hitid, &tuple, 1))
  453.     {
  454.  
  455.         if (strcmp(tuple.group, group))
  456.             continue;
  457.  
  458.         if (strcmp(tuple.delim, delim))
  459.             start = 1;
  460.  
  461.         if (start)
  462.         {
  463.             found = 1;
  464.             if (begin)
  465.                 begin = 0;
  466.             else
  467.                 print_delim(dstring);
  468.  
  469.             /*start a new string*/
  470.             strcpy(delim, tuple.delim);
  471.             start = 0;
  472.             *dstring = (char *)NULL;
  473.             strcat(dstring, tuple.delim);
  474.             strcat(dstring, ":        ");
  475.             convert_bitmap(dstring, &tuple);
  476.         }
  477.         else
  478.         {
  479.             /*add to old string*/
  480.             convert_bitmap(dstring, &tuple);
  481.         }
  482.     }
  483.     if (!found)
  484.         printf("group %s does not exist\n", group);
  485.     else
  486.         print_delim(dstring);
  487.     return(0);
  488. }
  489.  
  490. /*
  491. **    CONVERT_BITMAP - convert a bitmap back to a BNF expression
  492. **
  493. **        Parameters:
  494. **            dstring - string to stuff the BNF expression info
  495. **            tuple - tuple containing the bitmap
  496. **
  497. */
  498. convert_bitmap(dstring, tuple)
  499. char        *dstring;
  500. DELIM_TUP    *tuple;
  501. {
  502.     int    i,j;
  503.     char    *pntr;
  504.  
  505.     pntr = dstring + strlen(dstring);
  506.     *pntr++ = (tuple->type == ONE ? LBRACKET : LBRACE);
  507.     i = 0;
  508.     while (i < 128 )
  509.     {
  510.         if (test(tuple->bitmap, i))
  511.         {
  512.             *pntr++ = i;
  513.             j = ++i;
  514.             while ((j < 128) && test(tuple->bitmap, j))
  515.                 j++;
  516.             if ( (j - i) >= 5)
  517.             {
  518.                 j--;
  519.                 *pntr++ = '-';
  520.                 *pntr++ = j;
  521.                 i = j + 1;
  522.             }
  523.         }
  524.         else
  525.             i++;
  526.     }
  527.     *pntr++ = (tuple->type == ONE ? RBRACKET : RBRACE);
  528.     *pntr = (char *)NULL;
  529. }
  530.  
  531. /*
  532. **    PRINT_DELIM - print one delim of the rdelim relation
  533. **
  534. **        Parameters:
  535. **
  536. */
  537. print_delim(delim)
  538. char    *delim;
  539. {
  540.     printf("%s\n", delim);
  541. }
  542.  
  543. /*
  544. **    ALLDELIMS - print all the delims currently defined
  545. **
  546. */
  547. alldelims(des)
  548. DESC    *des;
  549. {
  550.     DELIM_TUP    tuple;
  551.     int        start=1;
  552.     char        group[12];
  553.     TID        lotid;
  554.     TID        hitid;
  555.  
  556.     printf("Delimitor groups:\n");
  557.     if (find(des,LRANGEKEY, &lotid, &hitid, group) < 0)
  558.         return(-1);
  559.     find(des,HRANGEKEY, &lotid, &hitid, group);
  560.  
  561.     while (!get(des, &lotid, &hitid, &tuple, 1))
  562.     {
  563.  
  564.         if (strcmp(tuple.group, group))
  565.             start = 1;
  566.  
  567.         if (start)
  568.         {
  569.             strcpy(group, tuple.group);
  570.             printf("\t\t %s\n", group);
  571.             /*helpdelim(group, des); */
  572.             start = 0;
  573.         }
  574.     }
  575. }
  576.